/*
 *  IOCOM.cpp
 *  Ndist
 *
 *  Created by Diego Andrade on 1/27/11.
 *  Copyright 2011 Carnegie Mellon University. All rights reserved.
 *
 */

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include "IOCOM.h"
#include "triangle.h"

#define PI 3.14159265


using namespace std;


triangle::triangle()
{
 next_p.x =0.0;
 next_p.y =0.0;
 
 growth_factor = 1.1;
 
 angle = 15.0; 
}

triangle::~triangle()
{


}

/*
Euclidean distance of two points
sqrt((x2-x1)^2+(y2-y1)^2)

*/

float triangle::distance(point p1, point p2)
{
    float d =0.0;
    d = sqrt(pow((p2.x-p1.x),2)+pow((p2.y-p1.y),2));
    return d;
    
}

/*
Tries to find randomly the next value for the 

*/

void triangle::random_point(float d, float gf, point p1, point p2)
{
   //srand(time(0));
   float M =0;
   float N =1;
   float r =  M + (float) rand() / (float)(RAND_MAX ) ;
   
   ///float r = (float) rand () / (float) RAND_MAX;
   
   next_p.x = (r)*(p2.x-p1.x);   //random number * growth factor * distance between 2 points
   next_p.y = (r)*(p2.y-p1.y);    
}


bool triangle::check_angle(float angle, point p1, point p2)
{
    bool response = true;
    float tvalue = 0.0;
    tvalue = (p2.y-p1.y)/(p2.x-p1.x);
    cout << "tvalue1 : " << tvalue <<endl;  
    
    tvalue = atan(tvalue)*180/PI;
 
    cout << "tvalue : " << tvalue <<endl;   
    
    if(tvalue < angle) response= false;
    return response; 

}

void triangle::newpoint (point p1, point p2)
{
    float d = distance( p1,  p2);
    random_point(d,growth_factor,p1,p2);
    
    
    
    bool response = check_angle(angle,p1,next_p);
    cout << "response p1-Nextp: " << response <<endl;   
    
    response = check_angle(angle,p2,next_p);
    cout << "response p2-Nextp: " << response <<endl;  
    
    
 

}

